import subprocess
import os

def run_mvn_install():
    # ======================
    # 1. 获取目录信息
    # ======================

    # 当前脚本所在目录
    script_dir = os.getcwd()
    print(f"📍 当前脚本目录: {script_dir}")

    # 上一级目录 = Maven 项目根目录（含 pom.xml）
    project_root = os.path.abspath(os.path.join(script_dir, ".."))
    print(f"🎯 Maven 项目根目录: {project_root}")


    # ======================
    # 2. 执行 Maven 打包
    # ======================

    mvn_command = ["mvn", "clean", "install", "-Dmaven.test.skip=true"]
    try:
        print("🚀 开始执行 Maven 命令: mvn clean install -Dmaven.test.skip=true")
        subprocess.run(mvn_command, cwd=project_root, check=True, text=True)
        print("✅ Maven 执行成功！")
    except subprocess.CalledProcessError as e:
        print("❌ Maven 执行失败！")
        print("错误码:", e.returncode)
        print("错误信息:")
        print(e.stderr)
        return
    except Exception as e:
        print("⚠️ 执行 Maven 时发生未知错误:", e)
        return

if __name__ == "__main__":
    run_mvn_install()